package sendEmail;

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.junit.Before;
import org.junit.Test;

public class SendEmail {
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
	private static final String SMTP_AUTH_USER = "denilson.dias.silva@gmail.com";
	private static final String SMTP_AUTH_PWD = "denil123";
	private static final String SMTP_PORT_NUM = "465";

	private ArrayList<String> to = new ArrayList<String>();
	private String from;
	private String subject;	
	private String content;

	@Before
	public void setUp() {
	}

	@Test
	public void sendMail() {
		if (to.size() > 0) {
			InternetAddress[] realToList = new InternetAddress[to.size()];
			Transport transport = null;
			Properties properties = System.getProperties();
			properties.put("mail.smtp.host", SMTP_HOST_NAME);
			properties.put("mail.smtp.user", SMTP_AUTH_USER);
			properties.put("mail.smtp.port", SMTP_PORT_NUM);
			properties.put("mail.smtp.socketFactory.port", SMTP_PORT_NUM);
			properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
			properties.put("mail.smtp.auth", "true");
			properties.put("mail.smtp.debug", "true");
			properties.put("mail.smtp.starttls.enable", "true");
			properties.put("javax.net.ssl.SSLSocketFactory", "false");

			Authenticator auth = new SMTPAuthenticator();
			Session session = Session.getDefaultInstance(properties, auth);

			MimeMessage message = new MimeMessage(session);

			try {
				message.setFrom(new InternetAddress(from));

				for (int i = 0; i < to.size(); i++) {
					realToList[i] = new InternetAddress(to.get(i));
				}

				message.setRecipients(Message.RecipientType.TO, realToList);
				message.setSubject(subject);
				message.setContent(content, "text/plain");
				// For HTML-Email
				// message.setContent(content, "text/html");

				transport = session.getTransport("smtp");
				transport.connect(SMTP_HOST_NAME, Integer
						.valueOf(SMTP_PORT_NUM).intValue(), SMTP_AUTH_USER,
						SMTP_AUTH_PWD);

				transport.sendMessage(message, message.getAllRecipients());
				System.out.println("Message sent successflly...");
			} catch (AddressException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					transport.close();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	private class SMTPAuthenticator extends Authenticator {
		@Override
		public PasswordAuthentication getPasswordAuthentication() {
			String username = SMTP_AUTH_USER;
			String password = SMTP_AUTH_PWD;

			return new PasswordAuthentication(username, password);
		}
	}

	public ArrayList<String> getTo() {
		return to;
	}

	public void toClear() {
		to.clear();
	}

	public void addTo(String to) {
		this.to.add(to);
	}
	
	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
}